<?php
$screen = Blyn::app()->getUIManager()->getCurrentScreen();
$wxControl = $screen->getChildControl('BUIWXControl_1');

$itemData = array();
$findTextArr = explode(' ',$findText);
foreach ($findTextArr as $value) {
    $condition = explode('=',$value);
    $itemData[$condition[0]] = $condition[1];
}
$itemData['bookingStatus'] = 'available';
//下面的两个条件非常重要：
$itemData['bookingStartTime'] = '<='.time();
$itemData['bookingEndTime'] = '>='.time();
    //only get item for current space
$itemData['spaceId'] = Blyn::app()->getActiveSpace()->getId();
//
//$itemData['startTime'] = '>='.time();
$treeTable = new BTreeTable('BWInputValue');
$valueList = $treeTable->getValueList('startTime', array('itemData' => $itemData));
//            var_dump($itemData);
//            echo '<br/>';
//            var_dump($valueList);

$count = count($valueList);
if($count < 10) {
    $findText = 'city=' . $itemData['city'] . ' courtType=' . $itemData['courtType'];
    $courtUrl = $wxControl->getLinkUrl(array(
        'name' => $flagText,
        'id' => 'booking_list'), array(
        'listPage' => 'booking_list_1',
        'findText' => $findText,
        'flagText' => $flagText,
        'nowCity' => $nowCity  //通常是省市拼音，如：china.chongqing
        ))
    ?>
    <script type="text/javascript">
        location.href = '<?php echo $courtUrl;?>';
    </script>
<?php }

$cs = Yii::app()->getClientScript();
$cs->registerCSSFile(Yii::app()->request->baseUrl . '/css/mobile/booking_find_1.css');
$cs->registerCSSFile(Yii::app()->request->baseUrl . '/css/mobile/booking_list.css');
$data = array();
if(isset($nowCity)) {
    $data['nowCity'] = $nowCity;    //获取用户当前所选的的城市，用于返回时确定城市
}
$findUrl = $wxControl->getLinkUrl(array('name' => 'find','id' => 'booking_find_1'),$data);
//$wxControl->renderView('booking_home', array()); 
?>
<?php
$data = array();
$data['htmlOptions'] = array('class' => 'btn btn-primary');
$findContext = array();
if (!isset($findContext) || !is_array($findContext))
    $findContext = array();
if (isset($findText))
    $findContext['findText'] = $findText;

Blyn::app()->getUIManager()->getCurrentScreen()->addContextVariable('findContext', $findContext);
?>
<meta name="viewport" content="initial-scale=1, width=device-width, maximum-scale=1, user-scalable=no">
<meta name="viewport" content="initial-scale=1.0,user-scalable=no,maximum-scale=1" media="(device-height: 568px)">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-touch-fullscreen" content="yes">
<meta name="full-screen" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="format-detection" content="telephone=no">
<link href="style/index.css" rel="stylesheet">
<script>
$("document").ready(function() {
    $(".booking_list > dd").click(function() {
        $(this).css("backgroundColor","rgb(243, 243, 246)");
        $(this).siblings().css("backgroundColor","white");
    });
}); 
</script>
<body>
<header class="navbar">
        <div class="nav-wrap-left">
            <a class="react back" href="<?php echo $findUrl;?>"><i class="text-icon icon-back"></i></a>
        </div>
    <h1 class="nav-header"><?php echo $flagText; ?></h1>

<?php
$wxControl->renderView('booking_navigation', array());
$weekArr = array(
    'Monday' => '星期一',
    'Tuesday' => '星期二',
    'Wednesday' => '星期三',
    'Thursday' => '星期四',
    'Friday' => '星期五',
    'Saturday' => '星期六',
    'Sunday' => '星期日',
);
?>
</header>

<div id="tips"></div>

<div class="deal-container">
    <div id="deals" class="deals-list">
        <div id="list_bar">
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <img src="<?php echo Yii::app()->request->baseUrl.'/images/mobile/dot.png';?>"/>
            <span class="bar_title">日期列表</span>
        </div>
        <dl class="booking_list">
            <?php 
            $dateListArr = array();
            foreach ($valueList as $date => $count) {
                $dateStr = date('Y-m-d', $date);
                if($dateStr == date('Y-m-d')) {
                    $displayName = '今天';
                }else if($dateStr == date('Y-m-d',strtotime('+1 day'))) {
                    $displayName = '明天';
                }else {
                    $displayName = $weekArr[date('l',$date)];
                }
                /**获取每天的场地数量，如：
                 * array(1) (
                 *      "2014-09-11"=> array("displayName"=> "今天" "count"=> 8 )
                 * )
                 */
                if(!isset($dateListArr[$dateStr])) {
                    $dateListArr[$dateStr] = array('displayName' => $displayName,'count' => intval($count));
                }else {
                    $dateListArr[$dateStr]['count'] += $count;
                }
            }
            $i = 0;
            $leftImageNO = 0;
            foreach ($dateListArr as $dateStr => $dateInfo) {
                $startTime = strtotime($dateStr.' 00:00:00');
                $endTime = $startTime + 24 * 60 * 60;
                $findText = 'city=' . $itemData['city'] . ' courtType=' . $itemData['courtType'] . ' startTime>=' . $startTime . ' endTime<=' . $endTime;
                $courtUrl = $wxControl->getLinkUrl(array(
                    'name' => $flagText,
                    'id' => 'booking_list'), array(
                    'listPage' => 'booking_list_by_timeSlot',
                    'findText' => $findText,
                    'flagText' => $flagText,
                    'dateStr'=> $dateStr,
                    'nowCity' => $nowCity  //通常是省市拼音，如：china.chongqing
                    ))
                ?>
                <dd style="border-bottom: .01rem solid #dedede;box-shadow: 0 .05rem .05rem #dedede;
                    margin-bottom:.15rem;
                    <?php /*if($i%2 != 0){echo 'background-color:#F3F3F3;';}else{*/echo 'background-color:white;';/*}*/?>">
                    <a href="<?php echo $courtUrl;?>">
                        <div class="item">
                            <table>
                                <tr>
                                    <td style="width: 40px;height: 60px;">
                                        <img src="<?php echo Yii::app()->request->baseUrl.'/images/mobile/left_image'.$leftImageNO.'.png';?>" width="100%" height="100%;"/>
                                    </td>
                                    <td style="width: 1.5rem;height: 60px;vertical-align: middle;">
                                        <span style="color:2BB2A3;font-size:.4rem;font-weight:bold;"><?php echo $dateInfo['displayName']; ?></span>
                                    </td>
                                    <td style="height: 60px;vertical-align: middle;">
                                        <img style="vertical-align:text-bottom;" src="<?php echo Yii::app()->request->baseUrl.'/images/mobile/datetime.png';?>"/>
                                        <span style="color:#AFAFAF;font-size:16px;"><?php echo date('Y.n.j',  strtotime($dateStr));?></span>
                                    </td>
                                    <td style="width:2.0rem;vertical-align:middle;">
                                        <span><a class="mbtn-trans" data-dealid="25150270">剩余场地：<font style=" font-family: fantasy;font-size: x-large; "><?php echo $dateInfo['count']; ?></font></a></span>
<!--                                        <div style="width:1.3rem;height: .36rem;padding-top:.04rem;background: url(<?php // echo Yii::app()->request->baseUrl.'/images/mobile/have.png';?>) no-repeat;background-size:100% 100%;">
                                            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                            <font style="color:white;font-size: 16px;"><?php // echo $dateInfo['count']; ?></font>
                                        </div>-->
                                        <!--<img src="<?php echo Yii::app()->request->baseUrl.'/images/mobile/right24.png';?>"/>-->
                                    </td>
                                </tr>
<!--                                <tr>
                                    <td style="height:40px;vertical-align: top;">
                                        <img style="vertical-align:text-bottom;" src="<?php echo Yii::app()->request->baseUrl.'/images/mobile/datetime.png';?>"/>
                                        <span style="color:#AFAFAF;font-size:16px;"><?php echo date('Y.n.j',  strtotime($dateStr));?></span>
                                        &nbsp;
                                        <img style="vertical-align:text-bottom;" src="<?php echo Yii::app()->request->baseUrl.'/images/mobile/remain.png';?>"/>
                                        <span style="color:#AFAFAF;font-size:16px;">还剩场地：<font style="color:red;"><?php echo $dateInfo['count']; ?></font>个</span>
                                    </td>
                                </tr>-->
<!--                                <tr style="height:30%;vertical-align: top;">
                                    <td style="">
                                        <img style="vertical-align:text-bottom;" src="<?php echo Yii::app()->request->baseUrl.'/images/mobile/remain.png';?>"/>
                                        <span style="color:#AFAFAF;font-size:16px;">还剩场地：<font style="color:red;"><?php echo $dateInfo['count']; ?></font>个</span>
                                    </td>
                                </tr>-->
                            </table>
                        </div>
                    </a>
                </dd>
            <?php 
                if($leftImageNO < 2)
                    $leftImageNO++;
                else 
                    $leftImageNO = 0;
                $i++;
            } 
            ?>
        </dl>
    </div>
    <div class="shade hide"></div>
</div>
<footer>
        <div class="footer-copyright">
            <div class="hr"></div>
            <span class="footer-copyright-text">
			Copyright © 2014 billyn.net </span>
        </div>
    </footer>
 </body></html>